package com.jinlia.show.modules.wxapp.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jinlia.show.modules.base.model.query.GoodsTypeQuery;
import com.jinlia.show.modules.base.system.entity.Goods;
import com.jinlia.show.modules.base.model.form.GoodsForm;
import com.jinlia.show.modules.base.model.po.GoodsPO;
import com.jinlia.show.modules.base.system.service.GoodsMapperService;
import com.jinlia.show.modules.wxapp.model.vo.DefaultGoodsAttrValue;
import com.jinlia.show.modules.wxapp.model.vo.GoodsTypeVO;
import com.jinlia.show.modules.wxapp.service.GoodsService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

/**
 * 映射页面 prod 和 prod-classify
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/goods")
public class GoodsController {

    @Autowired
    private GoodsService goodsService;

    @GetMapping(value = "/get/{id}")
    private GoodsPO getById(@PathVariable("id") Long id) {
        GoodsForm goodsForm = new GoodsForm();
        goodsForm.setGoodsId(id);
        List<GoodsPO> forOrder = goodsService.getBaseMapper().getVOList(goodsForm);
        GoodsPO goodsVO = forOrder.get(0);
        return goodsVO;
    }

    /**
     * 默认的商品属性
     *
     * @param id
     * @return
     */
    @GetMapping(value = "/get/default/{id}")
    private JSONObject getDefaultGoodsAttrValue(@PathVariable("id") Long id) {
        JSONObject defaultGoodsAttrValue = goodsService.getDefaultGoodsAttrValue(id);
        return defaultGoodsAttrValue;
    }

    @PostMapping("/update")
    public ResponseEntity<Boolean> updateSaleModel(@RequestBody Goods goods) {
        goodsService.updateSaleModel(goods);
        return ResponseEntity.ok(Boolean.TRUE);
    }

    @PostMapping("list")
    public ResponseEntity<IPage<GoodsPO>> goodsListPage(@RequestBody GoodsTypeQuery goodsTypeQuery) {
        Page<GoodsPO> goodsPOPage = goodsService.goodsListPage(goodsTypeQuery);
        return ResponseEntity.ok(goodsPOPage);
    }

    /**
     * 根据商品id获取热门组合组合
     */
    @GetMapping("/hotCombination/{id}")
    public ResponseEntity<List<GoodsPO>> getHotCombination(@PathVariable("id") Long id) {
        List<GoodsPO> combination = goodsService.getHotCombination(id);
        return ResponseEntity.ok(combination);
    }

    /**
     *
     */
    @GetMapping("/for/order")
    public ResponseEntity<List<GoodsTypeVO>> getForOrder() {
        List<GoodsTypeVO> forOrder = goodsService.getForOrder();
        return ResponseEntity.ok(forOrder);
    }
}
